var xmlHttp; //定义XMLHttpRequest对象
function createXmlHttpRequestObject() {
    //如果在internet Explorer下运行
    if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp = false;
        }

    } else {
        //如果在Mozilla或其他的浏览器下运行
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            xmlHttp = false;
        }
    }
    //返回创建的对象或显示错误信息
    if (!xmlHttp)
        alert("返回创建的对象或显示错误信息");
    else
        return xmlHttp;
}

function GetPetInfo() {
    createXmlHttpRequestObject();
    var select = document.getElementById("petlist");
    var selectedValue = select.options[select.selectedIndex].value;
    if (selectedValue != "none") {
        select.classList.remove("order-check-wrong");
    }
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            document.getElementById("petInfo").innerHTML = xmlHttp.responseText;
        }
    } //判断URL调用的状态值并处理
    xmlHttp.open("GET", 'u_getPetInfo.php?petlist=' + selectedValue, false);
    xmlHttp.send(null);
}



function GetDocInfo() {
    createXmlHttpRequestObject();

    var orderType = document.getElementById("orderType");
    var orderTypeValue = orderType.options[orderType.selectedIndex].value;
    if (orderTypeValue != "none") {
        orderType.classList.remove("order-check-wrong");
    }

    var orderDate = document.getElementById("orderDate");
    var orderDateValue = orderDate.value;
    var date = new Date(orderDateValue).getTime();//获取选择日期并转化为时间戳

    var nowDate = new Date();//获取当前日期
    var nowYear = nowDate.getFullYear();//获取当前年
    var nowMonth = nowDate.getMonth() + 1;//获取当前月
    var nowDay = nowDate.getDate();//获取当前日
    var nowHour = nowDate.getHours();//获取当前小时
    var now = new Date("'" + nowYear + "-" + nowMonth + "-" + nowDay + "'").getTime() + 8 * 60 * 60 * 1000;//获取当前时间戳

    var count = Math.ceil((date - now) / (1000 * 60 * 60 * 24)) > 7;//计算日期差值
    var index_orderType = document.getElementById("index_orderType");
    var index_orderDate = document.getElementById("index_orderDate");

    // 判断选择
    if (date < now || (date == now && nowHour > 18) || count) {
        orderDate.classList.add("order-check-wrong");
        orderDateValue = "";
    } else {
        if (orderDateValue != "")
            orderDate.classList.remove("order-check-wrong");
    }

    if (index_orderType.value != orderTypeValue || index_orderDate.value != orderDateValue) {
        index_orderType.value = orderTypeValue;
        index_orderDate.value = orderDateValue;
        xmlHttp.open("GET", 'u_getDocList.php?orderType=' + orderTypeValue + '&orderDate=' + orderDateValue, false);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                document.getElementById("docList-div").innerHTML = xmlHttp.responseText;
            }
        } //判断URL调用的状态值并处理
        xmlHttp.send(null);
    }
}

//表单提交检查
//是否选择宠物信息
function checkPetInfos() {
    var petlist = document.getElementById("petlist");
    var petID = petlist.options[petlist.selectedIndex].value;
    if (petID == "none") {
        petlist.classList.add("order-check-wrong");
        return false;
    } else {
        petlist.classList.remove("order-check-wrong");
        return true;
    }
}
//是否选择服务类别
function checkOrderType() {
    var orderType = document.getElementById("orderType");
    var orderTypeValue = orderType.options[orderType.selectedIndex].value;
    if (orderTypeValue == "none") {
        orderType.classList.add("order-check-wrong");
        return false;
    } else {
        orderType.classList.remove("order-check-wrong");
        return true;
    }
}
//是否选择预约日期
function checkDate() {
    var orderDate = document.getElementById("orderDate");
    var orderDateValue = orderDate.value;

    var nowDate = new Date();//获取当前日期
    var nowYear = nowDate.getFullYear();//获取当前年
    var nowMonth = nowDate.getMonth() + 1;//获取当前月
    var nowDay = nowDate.getDate();//获取当前日
    var nowHour = nowDate.getHours();//获取当前小时
    var now = new Date("'" + nowYear + "-" + nowMonth + "-" + nowDay + "'").getTime() + 8 * 60 * 60 * 1000;//获取当前时间戳
    var date = new Date(orderDateValue).getTime();//获取选择日期并转化为时间戳

    var count = Math.ceil((date - now) / (1000 * 60 * 60 * 24)) > 7;//计算日期差值
    if (date < now || (date == now && nowHour > 17) || orderDateValue == "" || count) {
        orderDate.classList.add("order-check-wrong");
        return false;
    } else {
        orderDate.classList.remove("order-check-wrong");
        return true;
    }
}
//是否选择预约时间 
function checkTime() {
    var orderDate = document.getElementById("orderDate");
    var orderDateValue = orderDate.value;
    var date = new Date(orderDateValue).getTime();//获取选择日期并转化为时间戳

    var nowDate = new Date();//获取当前日期
    var nowYear = nowDate.getFullYear();//获取当前年
    var nowMonth = nowDate.getMonth() + 1;//获取当前月
    var nowDay = nowDate.getDate();//获取当前日
    var nowHour = nowDate.getHours();//获取当前小时
    var now = new Date("'" + nowYear + "-" + nowMonth + "-" + nowDay + "'").getTime() + 8 * 60 * 60 * 1000;//获取当前时间戳

    var orderTime = document.getElementById("orderTime");
    var orderHour = Number(orderTime.value.substring(0, 2));

    // 判断预约具体时间跟现在的时间差是否在同一个小时
    if (orderTime.value == "" || orderHour < 8 || orderHour > 17 || (date == now && orderHour <= nowHour)) {
        orderTime.classList.add("order-check-wrong");
        return false;
    } else {
        orderTime.classList.remove("order-check-wrong");
        return true;
    }
}
//是否选择预约医生
function checkOrderDoc() {
    var orderDoc = "";
    // 获取所有的单选框
    var checkRadios = document.getElementsByName('docID');
    // 获取被选中的单选框的值
    for (var i = 0; i < checkRadios.length; i++) {
        if (checkRadios[i].checked) {
            orderDoc = checkRadios[i].value;
        }
    }

    if (checkRadios.length == 0 || orderDoc == "") {
        var orderDocTips = document.getElementById("orderDocTips");
        orderDocTips.classList.add("order-check-wrong");
        return false;
    } else {
        var orderDocTips = document.getElementById("orderDocTips");
        orderDocTips.classList.remove("order-check-wrong");
        return true;
    }
}

//点击单选框后取消可预约医生错误提示
function clickRadios() {
    var orderDocTips = document.getElementById("orderDocTips");
    orderDocTips.classList.remove("order-check-wrong");
}
//是否填写正确的联系方式 
function checkPhone() {
    var re = /^1[3-9]\d{9}$/;
    var phone = document.getElementById("phone");
    if (!re.test(phone.value)) {
        phone.classList.add("order-check-wrong");
        return false;
    } else {
        phone.classList.remove("order-check-wrong");
        return true;
    }
}

function checkAllForm() {
    checkPetInfos();
    checkOrderType();
    checkDate();
    checkTime();
    checkOrderDoc();
    checkPhone();
    if (checkPetInfos() && checkOrderType() && checkDate() && checkTime() && checkOrderDoc() && checkPhone()) {
        return true;
    } else {
        alert("请检查输入信息");
        return false;
    }
}